﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace YJKCodeBase.StringUtility
{
	public sealed class StrEscapeTool
	{
		public static string EscapeAndMerge(List<string> input)
		{
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < input.Count; i++)
			{
				stringBuilder.Append(StrEscapeTool.Escape(input[i]));
				if (i != input.Count - 1)
				{
					stringBuilder.Append(" ;");
				}
			}
			return stringBuilder.ToString();
		}

		public static List<string> UnEsapeAneUnMerge(string input)
		{
			List<string> list = new List<string>();
			string[] array = StrEscapeTool.m_UseUnMergeRegex.Split(input);
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				if (i != array.Length - 1)
				{
					list.Add(StrEscapeTool.UnEscape(text.Remove(text.Length - 1, 1)));
				}
				else
				{
					list.Add(StrEscapeTool.UnEscape(text));
				}
			}
			return list;
		}

		public static string Escape(string input)
		{
			return StrEscapeTool.EscapeMethod(input, true);
		}

		public static string UnEscape(string input)
		{
			return StrEscapeTool.EscapeMethod(input, false);
		}

		private static string EscapeMethod(string input, bool ifEscape)
		{
			StringBuilder stringBuilder = new StringBuilder();
			Regex regex = ifEscape ? StrEscapeTool.m_UseEscapeRegex : StrEscapeTool.m_UseUnEsacpeRegex;
			int num = 0;
			foreach (object obj in regex.Matches(input))
			{
				Match match = (Match)obj;
				stringBuilder.Append(input.Substring(num, match.Index - num));
				stringBuilder.Append(StrEscapeTool.ChangString(match.Value, ifEscape));
				num = match.Index + match.Length;
			}
			if (num <= input.Length - 1)
			{
				stringBuilder.Append(input.Substring(num, input.Length - num));
			}
			return stringBuilder.ToString();
		}

		private static string ChangString(string input, bool ifIsEscape)
		{
			string result;
			if (ifIsEscape)
			{
				result = input.Insert(0, "/");
			}
			else
			{
				result = input.Remove(0, 1);
			}
			return result;
		}

		private const string m_strGoal = ";";

		private const string m_strEscape = "/";

		private const string m_strWihte = " ";

		private static Regex m_UseEscapeRegex = new Regex("/*;");

		private static Regex m_UseUnEsacpeRegex = new Regex("/+;");

		private static Regex m_UseUnMergeRegex = new Regex("(?<!/);");
	}
}
